package com.wnxy.controller;

import com.github.pagehelper.PageInfo;
import com.wnxy.pojo.Article;
import com.wnxy.service.ArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/article")
@Api(tags = "文章管理")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @GetMapping
    @ApiOperation("查询全部")
    public List<Article> findAll(){
        return articleService.findAll();
    }

    @GetMapping("/page")
    @ApiOperation("分页查询全部")
    public PageInfo<Article> findByPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "3") Integer pageSize){

        PageInfo<Article> pageInfo = articleService.findByPage(pageNum,pageSize);
        return pageInfo;
    }

    @PostMapping
    @ApiOperation("添加或修改")
    public Boolean saveOrUpdate(@RequestBody Article article) {
        if (StringUtils.isEmpty(article.getId())) {
            // id 为空，执行添加
            articleService.add(article);
        } else {
            // id 不为空，说明是修改
            articleService.update(article);
        }
        return true;
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除")
    public Boolean delete(@PathVariable("id") Integer id){
        articleService.delete(id);
        return true;
    }

}















